package com.meixiang.view;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.HorizontalScrollView;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.meixiang.R;
import com.meixiang.util.DensityUtil;
import com.meixiang.util.GlideHelper;

import java.util.List;

/**
 * 画廊
 *
 * @author LZRUI
 */
public class ImageGallery extends HorizontalScrollView {

    private Context mContext;
    private LinearLayout container;
    private int marginLeft = 10;
    private int marginRight = 10;
    private int marginTop = 10;
    private int marginBottom = 10;

    public ImageGallery(Context context) {
        this(context, null);
    }

    public ImageGallery(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public ImageGallery(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mContext = context;
        container = new LinearLayout(mContext);
        container.setOrientation(LinearLayout.HORIZONTAL);
    }

    /**
     * 获取itemView，显示加号图标
     *
     * @param url        图片url
     * @param isLastItem 是否是最后一个item，true 是，false 不是
     */
    private void getView(String url, boolean isLastItem) {
        View itemView = LayoutInflater.from(mContext).inflate(R.layout.item_privilege_suit_image, container, false);
        ImageView imgView = (ImageView) itemView.findViewById(R.id.iv_goods_logo);
        GlideHelper.showImage(mContext, url, imgView);
        if (!isLastItem) {
            ImageView addView = (ImageView) itemView.findViewById(R.id.iv_add);
            addView.setVisibility(View.VISIBLE);
        }
        addItemView(itemView);
    }

    /**
     * 获取itemView，不显示加号图标
     *
     * @param url 图片url
     */
    private void getView(String url) {
        View itemView = LayoutInflater.from(mContext).inflate(R.layout.item_privilege_suit_image, container, false);
        ImageView imgView = (ImageView) itemView.findViewById(R.id.iv_goods_logo);
        GlideHelper.showImage(mContext, url, imgView);
        addItemView(itemView);
    }

    private void addItemView(View itemView) {
        LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        lp.setMargins(DensityUtil.dip2px(getContext(), this.marginLeft),
                DensityUtil.dip2px(getContext(), this.marginTop),
                DensityUtil.dip2px(getContext(), this.marginRight),
                DensityUtil.dip2px(getContext(), this.marginBottom));
        itemView.setLayoutParams(lp);
        container.addView(itemView);
    }

    /**
     * 设置数据
     *
     * @param urls       url列表
     * @param showAddImg 是否显示加号图标
     */
    public void setData(List<String> urls, boolean showAddImg) {
        removeAllViews();
        container.removeAllViews();
        int len = urls.size();
        if (showAddImg) {
            for (int i = 0; i < len; i++) {
                getView(urls.get(i), i == len - 1);
            }
        } else {
            for (String url : urls) {
                getView(url);
            }
        }
        addView(container);
    }

    /**
     * 设置边距
     *
     * @param left
     * @param top
     * @param right
     * @param bottom
     */
    public void setMargin(int left, int top, int right, int bottom) {
        this.marginLeft = left;
        this.marginRight = right;
        this.marginTop = top;
        this.marginBottom = bottom;
    }

    public void setOnClickListener(OnClickListener onClickListener) {
        if (container != null && onClickListener != null) {
            container.setOnClickListener(onClickListener);
        }
    }

}
